<template>
  <el-dialog
    :model-value="dialogVisible"
    title="相似文件关系统计图"
    @close="$emit('close')"
    width="80%"
    :close-on-click-modal="false"
    @opened="initChart"
  >
    <div ref="chartDom" style="width: 100%; height: 600px"></div>
    <template #footer>
      <el-button type="primary" @click="$emit('close')">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, onMounted, defineProps, defineEmits } from 'vue'
import * as echarts from 'echarts'

const props = defineProps({
  dialogVisible: {
    default: false,
    type: Boolean
  },
  filesData: {
    type: Array,
    required: true
  }
})

const emit = defineEmits(['close'])

const chartDom = ref(null)
let myChart = null

// 初始化图表
const initChart = () => {
  if (!props.filesData || props.filesData.length === 0) return // 确保有数据

  const classificationStats = {}
  const levelStats = {}
  const classificationFileNames = {}
  const levelFileNames = {}

  props.filesData.forEach((file) => {
    // 分类统计和文件名收集
    let classification = file.classification || file.handClassification
    let classificationName = file.classificationName || file.handClassificationName
    if (classification && classificationName) {
      if (!classificationStats[classificationName]) {
        classificationStats[classificationName] = 0
        classificationFileNames[classificationName] = []
      }
      classificationStats[classificationName]++
      classificationFileNames[classificationName].push(file.fileName) // 假设文件名为fileName字段
    }

    // 分级统计和文件名收集
    let level = file.Level || file.handLevel
    let levelName = file.LevelName || file.handLevelName
    if (level && levelName) {
      if (!levelStats[levelName]) {
        levelStats[levelName] = 0
        levelFileNames[levelName] = []
      }
      levelStats[levelName]++
      levelFileNames[levelName].push(file.fileName) // 假设文件名为fileName字段
    }
  })

  const classificationSeriesData = Object.entries(classificationStats).map(([name, value]) => ({
    name,
    value
  }))
  const levelSeriesData = Object.entries(levelStats).map(([name, value]) => ({ name, value }))

  const option = {
    title: [
      {
        text: '分类',
        left: 'center',
        top: '5%',
        left: '25%',
        textAlign: 'center'
      },
      {
        text: '分级',
        right: 'center',
        top: '5%',
        left: '75%',
        textAlign: 'center'
      }
    ],
    tooltip: {
      trigger: 'item',
      formatter: function (params) {
        if (params.seriesIndex === 0) {
          // 分类
          return `${params.name}<br/>数量：${params.value}<br/>包含文件：<br/>${classificationFileNames[params.name].join('<br/>')}`
        } else {
          // 分级
          return `${params.name}<br/>数量：${params.value}<br/>包含文件：<br/>${levelFileNames[params.name].join('<br/>')}`
        }
      }
    },
    legend: [
      {
        orient: 'vertical',
        left: 'left',
        data: Object.keys(classificationStats)
      },
      {
        orient: 'vertical',
        right: 'right',
        data: Object.keys(levelStats)
      }
    ],
    series: [
      {
        name: '分类',
        type: 'pie',
        radius: ['30%', '50%'],
        center: ['25%', '55%'], // 调整中心位置以适应标题
        data: classificationSeriesData,
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)'
          }
        }
      },
      {
        name: '分级',
        type: 'pie',
        radius: ['30%', '50%'],
        center: ['75%', '55%'], // 调整中心位置以适应标题
        data: levelSeriesData,
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)'
          }
        }
      }
    ]
  }

  // 初始化echarts实例
  myChart = echarts.init(chartDom.value)

  // 使用刚指定的配置项和数据显示图表。
  try {
    myChart.setOption(option)
  } catch (error) {
    console.error('Error setting ECharts option:', error)
  }
}

// 清理图表实例，避免内存泄漏
onMounted(() => {
  return () => {
    if (myChart) {
      myChart.dispose()
    }
  }
})
</script>

<style lang="scss" scoped>
/* 可根据需要添加样式 */
</style>
